//
//  MCSectionBar.m
//  MClient
//
//  Created by zhouyi on 10-11-10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCSectionBar.h"

@implementation MCSectionBar

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		self.barStyle = UIBarStyleDefault;
		MCUserDefaults *userDefault = [MCUserDefaults instance];
		self.tintColor = userDefault.skin_segmentedControlColor;
    }
    return self;
}

- (void)addSectionBarItems:(NSMutableArray* )items delegate:aDelegate {
	float sectionWidth = 90.0;
	CGRect scrollFrame = CGRectMake([items count]*sectionWidth - CGRectGetWidth(self.frame)>0?10:(CGRectGetWidth(self.frame) - [items count]*sectionWidth)/2, 
									3, 
									[items count]*sectionWidth - CGRectGetWidth(self.frame)>0?CGRectGetWidth(self.frame) - 20:[items count]*sectionWidth, 
									30);
	UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
	scrollView.backgroundColor = [UIColor clearColor];
	scrollView.scrollEnabled = YES;
	scrollView.showsHorizontalScrollIndicator = NO;
	scrollView.contentSize = CGSizeMake([items count]*sectionWidth, 30);
	scrollView.autoresizingMask = self.autoresizingMask;
	scrollView.tag = -876;
	
	
	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, [items count]*sectionWidth, 30)];
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
	
	MCUserDefaults *userDefault = [MCUserDefaults instance];
	segmentedControl.tintColor = userDefault.skin_segmentedControlColor;
	[segmentedControl addTarget:aDelegate action:@selector(changeSection:) forControlEvents:UIControlEventValueChanged];
	int i = 0;
	for (NSMutableDictionary *item in items) {
		NSString *itemLabel = [item objectForKey:@"lbl"];
		if ([itemLabel length] > 6) {
			itemLabel = [NSString stringWithFormat:@"%@..",[itemLabel substringToIndex:6]]; 
		}
		[segmentedControl insertSegmentWithTitle:itemLabel atIndex:i animated:NO];
		i++;
	}
	segmentedControl.selectedSegmentIndex = 0;
	
	[scrollView addSubview:segmentedControl];
	[segmentedControl release];

	[self addSubview:scrollView];
	[scrollView release];
}

- (void)addSectionBar:(NSString*)aTitle withItems:(NSMutableArray* )items delegate:aDelegate
{
	float sectionWidth = 90.0;
	CGRect scrollFrame = CGRectMake(([items count]+1)*sectionWidth - CGRectGetWidth(self.frame)>0?10:(CGRectGetWidth(self.frame) - ([items count]+1)*sectionWidth)/2, 
									3, 
									([items count]+1)*sectionWidth - CGRectGetWidth(self.frame)>0?CGRectGetWidth(self.frame) - 20:([items count]+1)*sectionWidth, 
									30);
	UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
	scrollView.backgroundColor = [UIColor clearColor];
	scrollView.scrollEnabled = YES;
	scrollView.showsHorizontalScrollIndicator = NO;
	scrollView.contentSize = CGSizeMake(([items count]+1)*sectionWidth, 30);
	scrollView.autoresizingMask = self.autoresizingMask;
	scrollView.tag = -876;
	
	MCUserDefaults *userDefault = [MCUserDefaults instance];
	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, ([items count]+1)*sectionWidth, 30)];
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
	segmentedControl.tintColor = userDefault.skin_segmentedControlColor;
	[segmentedControl addTarget:aDelegate action:@selector(changeSection:) forControlEvents:UIControlEventValueChanged];
	
	int index = 0;
	NSString *titleLabel = aTitle;
	if ([titleLabel length] > 6) {
		titleLabel = [NSString stringWithFormat:@"%@..",[titleLabel substringToIndex:6]]; 
	}
	[segmentedControl insertSegmentWithTitle:titleLabel atIndex:index++ animated:NO];
	
	for (NSMutableDictionary *item in items) {
		NSString *itemLabel = [item objectForKey:@"lbl"];
		if ([itemLabel length] > 6) {
			itemLabel = [NSString stringWithFormat:@"%@..",[itemLabel substringToIndex:6]]; 
		}
		[segmentedControl insertSegmentWithTitle:itemLabel atIndex:index++ animated:NO];
	}
	segmentedControl.selectedSegmentIndex = 0;
	
	[scrollView addSubview:segmentedControl];
	[segmentedControl release];
	[self addSubview:scrollView];
	[scrollView release];
}

- (void)dealloc {
    [super dealloc];
}

- (void)refreshFrame:(CGRect)frame
		  itemsCount:(int)itemsCount {
	self.frame = frame;
	float sectionWidth = 90.0;
	UIScrollView *scrollView = (UIScrollView *)[self viewWithTag:-876];
	if (scrollView) {
		CGRect scrollFrame = CGRectMake(itemsCount*sectionWidth - CGRectGetWidth(self.frame)>0?10:(CGRectGetWidth(self.frame) - itemsCount*sectionWidth)/2, 
										3, 
										itemsCount*sectionWidth - CGRectGetWidth(self.frame)>0?CGRectGetWidth(self.frame) - 20:itemsCount*sectionWidth, 
										30);
		scrollView.frame = scrollFrame;
	}
}

@end
